package gregoriancalendar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Fang Ruichuan
 * @date 2022-12-26 15:31
 */

public class Demo1 {
    public static void main(String[] args) {
        // 使用系统当前时间来构造对象
        Calendar cal = new GregorianCalendar();
        // 得到相同时间的Date对象
        Date now = cal.getTime();
        System.out.println("当前时间：" + now);
        // 输出各个字段
        System.out.println("年: " + cal.get(Calendar.YEAR));
        System.out.println("月: " + cal.get(Calendar.MONTH));
        System.out.println("日: " + cal.get(Calendar.DATE));
        System.out.println("上午_下午: " + cal.get(Calendar.AM_PM));
        System.out.println("小时: " + cal.get(Calendar.HOUR));
        System.out.println("一天中第几个小时（0-23）: " + cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("分钟: " + cal.get(Calendar.MINUTE));
        System.out.println("秒钟: " + cal.get(Calendar.SECOND));
        System.out.println("重新设置5个小时后: ");
        cal.set(Calendar.HOUR, 5);

        System.out.println("小时: " + cal.get(Calendar.HOUR));
        System.out.println("一天中第几个小时(0-23): " + cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("返回从起点至现在所经过的时间（毫秒）: " + cal.getTimeInMillis());
        System.out.println();

        // 使用年月日来构造对象
        Calendar cal2 = new GregorianCalendar(2020, 1, 1);
        // 得到相同时间的Date对象
        Date now2 = cal2.getTime();
        System.out.println("输出设置的时间: " + now2);

        // 使用年月日时分秒来构造对象
        Calendar cal3 = new GregorianCalendar(2020, 3, 1, 6, 6, 6);
        // 得到相同时间的Date对象
        Date now3 = cal3.getTime();
        System.out.println("输出设置的时间: " + now3);
    }
}
